package com.liezh.strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过传入的分享类型，提供分享服务
 * @author liezh
 * @date 2020-7-20
 */
public class ShareProvider {
    private List<ShareContext> shareContexts;

    {
        shareContexts = new ArrayList<ShareContext>();
        ShareContext qq = new ShareContext("qq", new QQShareStrategy());
        ShareContext sina = new ShareContext("sina", new SinaShareStrategy());
        shareContexts.add(qq);
        shareContexts.add(sina);
    }

    public boolean shareMethod(String type, String val) {
        ShareStrategy shareStrategy = null;
        for (ShareContext shareContext : this.shareContexts) {
            if (shareContext.options(type)) {
                shareStrategy = shareContext.getStrategy();
                break;
            }
        }
        if (shareStrategy == null) {
            return false;
        }
        shareStrategy.share(val);
        return true;
    }


}
